﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace ZhwangTool
{
    class ImageConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                string path = value.ToString();

                if (File.Exists(path))
                {
                    //BinaryReader binaryReader = new BinaryReader(File.Open(path, FileMode.Open));

                    //FileInfo fileInfo = new FileInfo(path);


                    //byte[] bytes = binaryReader.ReadBytes((int)fileInfo.Length);

                    //binaryReader.Close();

                    //System.Drawing.Bitmap bmTemp = new Bitmap(new MemoryStream(bytes));

                    //Bitmap bmNew = new Bitmap(bmTemp.Width, bmTemp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    //bmNew.SetResolution(96, 96);

                    //using (Graphics g = Graphics.FromImage(bmNew))
                    //{
                    //    g.Clear(System.Drawing.Color.Transparent);
                    //    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    //    g.DrawImage(bmTemp, new Rectangle(0, 0, bmNew.Width, bmNew.Height), 0, 0, bmTemp.Width, bmTemp.Height, GraphicsUnit.Pixel);
                    //    g.Dispose();
                    //}
                    //BitmapImage bitmapImage = new BitmapImage();
                    //using (MemoryStream ms = new MemoryStream())
                    //{
                    //    bmNew.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                    //    bitmapImage.BeginInit();
                    //    bitmapImage.StreamSource = ms;
                    //    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                    //    bitmapImage.EndInit();

                    //    ms.Dispose();
                    //}


                    BitmapImage bitmapImage = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));


                    return bitmapImage;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
}
